package UIFree.controls
{
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class uif_leavesBlock extends uif_maskedArea {
		private var _block		: uif_ecomponent;
		private var _showState	: Number;
		private var _align		: String;
		
		public static const LEFTALIGN: String 	= 'left';
		public static const RIGHTALIGN: String 	= 'right';
		public static const TOPALIGN: String 	= 'top';
		public static const BOTTOMALIGN: String = 'bottom';
		
		public function uif_leavesBlock(parent:uif_component, ax:int=0, ay:int=0, 
										awidth:int=0, aheight:int=0, styleClass:String='', a_align: String=LEFTALIGN) {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_align = style['align']?style['align']:a_align;
			var inner: Rectangle = getInner();
			_block = new uif_ecomponent(this, inner.x, inner.y, inner.width, inner.height, style['block']);
			_showState = minState;
			updateBlockPos();
		}
		
		public function get align(): String {
			return _align;
		}
		
		public function set align(a_align: String): void {
			_showState = 1;
			updateBlockPos();
			_align = a_align;
			_showState = minState;
			updateBlockPos()
		}
		
		public function get showState(): Number {
			return _showState;
		}
		
		public function set showState(a_state: Number): void {
			_showState = a_state;
			updateBlockPos();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function get block(): uif_ecomponent {
			return _block;
		}
		
		protected function updateBlockPos(): void {
			if (_align) {
				var rect: Rectangle = getInner();
				switch (_align) {
					case LEFTALIGN	: _block.x = rect.x - (_block.width + rect.x) * (1 - _showState);  
								break;
					case TOPALIGN	: _block.y = rect.y - (_block.height + rect.y) * (1 - _showState);  
								break;
					case RIGHTALIGN	: _block.x = rect.x + _block.width  * (1 - _showState);  
								break;
					case BOTTOMALIGN : _block.y = rect.y + _block.height * (1 - _showState);  
								break;
				}
			}
		}
		
		public function show(doEndShow: Function=null): void {
			fx('showState', showState, maxState).start(doEndShow);
		}
		
		public function hide(doEndHide: Function=null): void {
			fx('showState', showState, minState).start(doEndHide);
		}
		
		protected function get minState(): Number {
			return 0;
		}
		
		protected function get maxState(): Number {
			return 1;
		}
		
		override protected function updateBounds(): uif_component {
			super.updateBounds();
			if (_block) {
				var inner: Rectangle = getInner();
				_block.resize(inner.width, inner.height);
			}
			return this;
		}
	}
}